<template>
  <el-dialog
    :title="title"
    :visible.sync="dialog.isShow"
    width="831px"
    top="70px"
    :close-on-click-modal="false"
    @open="openDialog"
  >
    <div class="calendar-add">
      <div class="el-dialog-div">
        <el-scrollbar wrap-class="scrollbar-wrapper">
          <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-width="80px" inline label-position="top">
            <el-form-item label="会计年度" prop="intyear">
              <el-date-picker
                v-model="addForm.intyear"
                type="year"
                placeholder="选择年"
                :style="{width:itemWidth}"
                :picker-options="pickerOptions"
                value-format="yyyy"
                @change="(e) => changeYear(e)"
              />
            </el-form-item>
            <el-form-item label="开始日期" prop="strstartdate">
              <el-date-picker
                v-model="addForm.strstartdate"
                type="date"
                :disabled="addForm.blnnaturalmonth === 1 ? true : false"
                placeholder="选择开始日期"
                value-format="yyyy-MM-dd"
                @change="(e) => changeYearStart(e)"
              />
            </el-form-item>
            <el-form-item label="结束日期" prop="strenddate">
              <el-date-picker
                v-model="addForm.strenddate"
                type="date"
                :disabled="addForm.blnnaturalmonth === 1 ? true : false"
                value-format="yyyy-MM-dd"
                placeholder="选择结束日期"
                @change="(e) => changeYearEnd(e)"
              />
            </el-form-item>
            <el-form-item label="账套启用日期" prop="stropendate">
              <el-date-picker
                v-model="addForm.stropendate"
                type="date"
                placeholder="选择账套启用日期"
                value-format="yyyy-MM-dd"
                :disabled="opendateDisabled"
                @change="(e) => changeOpenDate(e)"
              />
            </el-form-item>
            <el-form-item label="结账日" prop="straccountdate">
              <el-select v-model="addForm.straccountdate" :disabled="addForm.blnnaturalmonth === 1" placeholder="选择结账日" @change="changeClosingDate">
                <el-option v-for="item in straccountdateList" :key="item" :value="item">
                  {{ item }}
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="" prop="strfundsourcename">
              <el-checkbox v-model="blnnaturalmonth" @change="changeMouth">自然月</el-checkbox>
            </el-form-item>
          </el-form>
          <div class="">
            <el-tabs size="mini" type="card" :style="{background:'#F5F4F7'}">
              <el-tab-pane label="会计期间">
                <el-table
                  :header-cell-style="{background:'#F5F4F7'}"
                  :data="tenlist"
                  border
                  size="mini"
                  fit
                  stripe
                  highlight-current-row
                  style="width: 100%;"
                  :default-sort="{prop: 'strfundsourcecode', order: 'ascending'}"
                >
                  <el-table-column
                    type="index"
                    width="60"
                    label="序号"
                    align="center"
                  >
                    <template v-slot="scope">
                      {{ scope.$index + 1 }}
                    </template>
                  </el-table-column>
                  <el-table-column label="期间" min-width="130px" prop="bytperiod">
                    <template slot-scope="scope">
                      <span>第{{ scope.row.bytperiod }}期间</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="开始日期" min-width="130px">
                    <template slot-scope="scope">
                      <span>{{ scope.row.strstartdate }}</span>
                    </template>
                  </el-table-column>

                  <el-table-column label="结束日期" min-width="130px">
                    <template slot-scope="scope">
                      <span>{{ scope.row.strenddate }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="状态" width="100px" align="center">
                    <template slot-scope="scope">
                      <span>{{ scope.row.blnisinactive === 0 ? '启用':" 停用" }}</span>
                    </template>
                  </el-table-column>
                </el-table>
              </el-tab-pane>
              <el-tab-pane label="季度">
                <el-table
                  :header-cell-style="{background:'#F5F4F7'}"
                  :data="quarterList"
                  border
                  size="mini"
                  fit
                  stripe
                  highlight-current-row
                  style="width: 100%;"
                  :default-sort="{prop: 'strfundsourcecode', order: 'ascending'}"
                >
                  <el-table-column
                    type="index"
                    width="60"
                    label="序号"
                    align="center"
                  >
                    <template v-slot="scope">
                      {{ scope.$index + 1 }}
                    </template>
                  </el-table-column>
                  <el-table-column label="季度" min-width="130px" prop="strperiod">
                    <template slot-scope="scope">
                      <span>{{ scope.row.strperiod }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="开始期间" min-width="130px">
                    <template slot-scope="scope">
                      <span>{{ scope.row.strstartperiod }}</span>
                    </template>
                  </el-table-column>

                  <el-table-column label="结束期间" min-width="130px">
                    <template slot-scope="scope">
                      <span>{{ scope.row.strendperiod }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="开始日期" min-width="130px">
                    <template slot-scope="scope">
                      <span>{{ scope.row.strstartdate }}</span>
                    </template>
                  </el-table-column>

                  <el-table-column label="结束日期" min-width="130px">
                    <template slot-scope="scope">
                      <span>{{ scope.row.strenddate }}</span>
                    </template>
                  </el-table-column>
                </el-table>
              </el-tab-pane>
              <el-tab-pane label="半年">
                <el-table
                  :header-cell-style="{background:'#F5F4F7'}"
                  :data="yearList"
                  border
                  fit
                  size="mini"
                  stripe
                  highlight-current-row
                  style="width: 100%;"
                  :default-sort="{prop: 'strfundsourcecode', order: 'ascending'}"
                >
                  <el-table-column
                    type="index"
                    width="60"
                    label="序号"
                    align="center"
                  >
                    <template v-slot="scope">
                      {{ scope.$index + 1 }}
                    </template>
                  </el-table-column>
                  <el-table-column label="半年" min-width="130px" prop="strperiod">
                    <template slot-scope="scope">
                      <span>{{ scope.row.strperiod }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="开始期间" min-width="130px">
                    <template slot-scope="scope">
                      <span>{{ scope.row.strstartperiod }}</span>
                    </template>
                  </el-table-column>

                  <el-table-column label="结束期间" min-width="130px">
                    <template slot-scope="scope">
                      <span>{{ scope.row.strendperiod }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="开始日期" min-width="130px">
                    <template slot-scope="scope">
                      <span>{{ scope.row.strstartdate }}</span>
                    </template>
                  </el-table-column>

                  <el-table-column label="结束日期" min-width="130px">
                    <template slot-scope="scope">
                      <span>{{ scope.row.strenddate }}</span>
                    </template>
                  </el-table-column>
                </el-table>
              </el-tab-pane>
            </el-tabs>
          </div>
        </el-scrollbar>
      </div>
    </div>
    <div slot="footer">
      <el-button type="primary" @click="saveCalendar(addForm)">确 定</el-button>
      <el-button @click="dialog.isShow = false">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { saveCalendar, getAddMessage } from '@/api/system/global-setting/calendar'
export default {
  name: 'CalendarSettingDialog',
  data() {
    const modelForm = {
      intyear: '', // 会计年度
      strstartdate: '2020-01-01', // 开始日期
      strenddate: '2020-12-31', // 结束日期
      stropendate: '2020-01-01', // 账套启用日期
      straccountdate: '', // 结账日期
      blnnaturalmonth: 0 // 自然月
    }
    const validcodeName = (rule, value, callback) => {
      if (value === '' & this.addForm.blnnaturalmonth === 0) {
        callback(new Error('必填项'))
      } else {
        callback()
      }
    }
    return {
      itemWidth: '233px',
      list: [],
      tenlist: [],
      stropendateDisable: false,
      quarterList: [],
      yearList: [],
      blnnaturalmonth: false,
      dialog: {
        isShow: false
      },
      // 设置日期的可选择范围
      pickerOptions: {
        disabledDate(time) {
          const newyeaer = new Date(Date.now() - 8.64e7).getFullYear() - 1
          const newyars = newyeaer + '-' + '01' + '-01'
          return time.getTime() < new Date(newyars).getTime()
        }
      },
      opendateDisabled: false,
      straccountdateList: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28],
      lngfundsourceid: '',
      title: '新增',
      addForm: modelForm,
      initAddForm: Object.assign({}, modelForm),
      addFormRules: {
        intyear: [
          { required: true, message: '必填字段', trigger: 'blur' }
        ],
        strstartdate: [
          { required: true, message: '必填字段', trigger: 'blur' }
        ],
        strenddate: [
          { required: true, message: '必填字段', trigger: 'blur' }
        ],
        stropendate: [
          { required: true, message: '必填字段', trigger: 'blur' }
        ],
        straccountdate: [
          { required: true, validator: validcodeName, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    show() {
      this.dialog.isShow = true
    },
    changeMouth(e) {
      this.blnnaturalmonth = e
      this.addForm.blnnaturalmonth = e ? 1 : 0
      if (e) {
        this.addForm.straccountdate = ''
      }
      if (this.addForm.intyear === '') {
        this.$message.error('请选择年份')
      }
      if (e && this.addForm.intyear !== '') {
        this.addForm.strstartdate = this.addForm.intyear + '-01-01'
        this.addForm.strenddate = this.addForm.intyear + '-12-31'
        this.calculationDateZ()
      } else {
        this.tenlist = []
        this.quarterList = []
        this.yearList = []
      }
    },
    changeYear(e) {
      // console.log(e, 'nian', this.addForm.intyear)
    },
    changeClosingDate() {
      if (this.blnnaturalmonth) {
        this.calculationDateZ()
      } else {
        this.calculationDate()
      }
    },
    changeOpenDate(e) {
      this.calculationDate()
    },
    // 判断账套启用日期是否发育开始日期以及结束日期
    setOpenDate() {
      if (this.addForm.strstartdate !== '' && this.addForm.strenddate !== '' && this.addForm.stropendate !== '') {
        if (!this.opendateDisabled) {
          if (this.dateType(this.addForm.stropendate, this.addForm.strstartdate).show === false || this.dateType(this.addForm.strenddate, this.addForm.stropendate).show === false) {
            this.$message.error('账套开始日期必须大于开始日期且小于结束日期')
          }
        }
      }
    },
    // 获取上期的参数日期
    getAddMessage() {
      getAddMessage().then((res) => {
        if (res.data !== '' && res.data !== {} && res.data !== null) {
          this.addForm.intyear = '' + (res.data.intyear + 1) + ''
          this.addForm.strstartdate = this.getNewDate2(res.data.strenddate)
          this.addForm.strenddate = new Date(res.data.strenddate).getFullYear() + 1 + '-' + this.iszeao(new Date(res.data.strenddate).getMonth() + 1) + '-' + this.iszeao(new Date(res.data.strenddate).getDate())
          this.addForm.stropendate = res.data.stropendate
          if (res.data.stropendate !== '') {
            this.opendateDisabled = true
          }
          if (res.data.straccountdate !== null) {
            this.addForm.straccountdate = res.data.straccountdate
            this.calculationDate()
          }
          if (res.data.blnnaturalmonth) {
            this.addForm.blnnaturalmonth = res.data.blnnaturalmonth
            if (res.data.blnnaturalmonth === 1) {
              this.blnnaturalmonth = true
              this.calculationDateZ()
            } else if (res.data.blnnaturalmonth === 0) {
              this.blnnaturalmonth = false
              this.calculationDate()
            }
          }
        } else {
          this.opendateDisabled = false
          this.blnnaturalmonth = false
          this.addForm.intyear = '' + (new Date().getFullYear()) + ''
          this.addForm.strstartdate = (new Date().getFullYear()) + '-' + '01' + '-' + '01'
          this.addForm.strenddate = '' + (new Date().getFullYear()) + '-12-31'
          this.addForm.stropendate = (new Date().getFullYear()) + '-' + '01' + '-' + '01'
        }
      })
    },
    // 此函数计算出 最大可选择的日期范围，
    getendDate() {
      let days = 0
      let daysEnd = 0
      let startMouth = 1 // 月份开始
      let statYear = ''
      let stateDate = 0
      //  如果开始日期不为空, 通过开始日期自动计算12个区间, 如果后去的最后日期大于结账日期需要再添加1个月，如果月份大于12则年份需要+1
      if (this.addForm.strstartdate !== '') {
        var stateOne = new Date(this.addForm.strstartdate)
        startMouth = stateOne.getMonth() + 1
        statYear = stateOne.getFullYear()
        stateDate = stateOne.getDate()
        if (stateDate > this.addForm.straccountdate) {
          startMouth = startMouth + 1
          if (startMouth + 1 > 12) {
            startMouth = 1
            statYear = statYear + 1
          }
        }
      }
      // 计算出最大的月份年份以及天
      for (let i = 0; i <= 12; i++) {
        if (i < 12) {
          if (startMouth + i > 12) {
            days = days + this.isday(startMouth + i - 12, statYear + 1)
            daysEnd = daysEnd + this.isday(startMouth + i - 12, statYear + 1)
          } else {
            days = days + this.isday(startMouth + i, statYear)
            daysEnd = daysEnd + this.isday(startMouth + i, statYear)
          }
        } else {
          if (startMouth + i > 12) {
            daysEnd = daysEnd + this.isday(startMouth + i - 12, statYear + 1)
          } else {
            daysEnd = daysEnd + this.isday(startMouth + i, statYear)
          }
        }
      }
      // 拼合成完整的年月日日期
      const jzrdatas = statYear + '-' + this.iszeao(startMouth) + '-' + (this.blnnaturalmonth === false ? this.iszeao(this.addForm.straccountdate) : this.isday(startMouth, statYear))
      // 将参数拼接完成后返回
      const canshu = {
        dates: this.getNewDate(jzrdatas, days),
        maxDates: this.getNewDate(jzrdatas, daysEnd)
      }
      if (stateDate < this.addForm.straccountdate) {
        const newdates = new Date(canshu.dates)
        const newdatsYue = newdates.getMonth() + 1
        const newdatasy = newdates.getFullYear()
        const newdatasyr = newdates.getDate()
        let newdatasa = ''
        if (newdates.getMonth() === 0) {
          newdatasa = (newdatasy - 1) + '-12-' + this.isday(12, (newdatasy - 1))
        } else {
          newdatasa = newdatasy + '-' + this.iszeao(newdatsYue) + '-' + this.isday(newdatsYue, newdatasy)
        }
        if (newdatsYue === 1) {
          newdatasa = (newdatasy - 1) + '-' + '12' + '-' + this.iszeao(newdatasyr)
        } else {
          newdatasa = (newdatasy) + '-' + this.iszeao(newdatsYue - 1) + '-' + this.iszeao(newdatasyr)
        }
        canshu.dates = newdatasa
        canshu.maxDates = this.getNewDate(jzrdatas, (days - 1))
      } else {
        const newmous = new Date(canshu.dates).getFullYear() // 获取参数年
        const yewsnwe = new Date(canshu.dates).getMonth() + 1 // 获取参数月
        const yewsdate = new Date(canshu.dates).getDate() // 获取参数日
        const newmousmaxDates = new Date(canshu.maxDates).getFullYear() // 获取参数最大年
        const yewsnwemaxDates = new Date(canshu.maxDates).getMonth() + 1 // 获取参数最大月
        const yewsdatemaxDates = new Date(canshu.maxDates).getDate() // 获取参数最大日
        if (yewsnwe - 1 > 0) {
          canshu.dates = newmous + '-' + this.iszeao(yewsnwe - 1) + '-' + this.iszeao(yewsdate)
        } else {
          canshu.dates = (newmous - 1) + '-' + '12' + '-' + this.iszeao(yewsdate)
        }
        if (yewsnwemaxDates - 1 > 0) {
          canshu.maxDates = newmousmaxDates + '-' + this.iszeao(yewsnwemaxDates - 1) + '-' + this.iszeao(yewsdatemaxDates)
        } else {
          canshu.maxDates = (newmousmaxDates - 1) + '-' + '12' + '-' + this.iszeao(yewsdatemaxDates)
        }
      }
      return canshu
    },
    // 设置非自然月日期生成方法
    calculationDate() {
      let statYear = '' // 开始年份)
      let startMouth = 1 // 月份开始
      let stateDate = '' // 日期开始
      let endMouth = '' // 月份结束
      let endDate = '' // 日期结束
      let beginTime = 0 // 初始化日期
      let endtime = 0 // 最后的结束日期
      // 判断会计日历结账日期是否优质
      if (this.blnnaturalmonth === false && this.addForm.straccountdate === '') {
        this.$message.error('请选择结账日')
        return false
      }
      // 获取开始日期各种日子
      if (this.addForm.strstartdate !== '') {
        var stateOne = new Date(this.addForm.strstartdate)
        statYear = stateOne.getFullYear()
        startMouth = stateOne.getMonth() + 1
        stateDate = stateOne.getDate()
        if (stateDate > this.addForm.straccountdate) {
          startMouth = startMouth + 1
          if (startMouth + 1 > 12) {
            startMouth = 1
            statYear = statYear + 1
          }
        }
      }
      // 获取结束日期各种日子
      if (this.addForm.strenddate !== '') {
        var endOne = new Date(this.addForm.strenddate)
        endMouth = endOne.getMonth() + 1
        endDate = endOne.getDate()
        console.log(endMouth, endDate, endtime)
      }
      // 账套日期日子
      // 比较开始日期是否大于结账日期，如果大于就从下一个月开始，如果不开始就从当月开始, 获取最后12个月的日期
      if (stateDate > this.addForm.straccountdate) {
        beginTime = startMouth
        endtime = beginTime + 12
      } else {
        beginTime = startMouth
      }
      if (this.dateType(this.addForm.strenddate, this.getendDate().dates).show && this.dateType(this.getendDate().maxDates, this.addForm.strenddate).show) {
        this.setOpenDate()
        this.tenlist = [] // 月度的保存列表
        this.quarterList = [] // 季度的保存列表
        this.yearList = [] // 半年度的保存列表
        const jzrdata = statYear + '-' + this.iszeao(startMouth) + '-' + (this.blnnaturalmonth === false ? this.iszeao(this.addForm.straccountdate) : this.isday(startMouth, statYear))
        let days = 0 // 设置日期
        let olddays = 0 // 设置根据这个数字设置年度季度月度对应日期
        let olddays2 = 0 // 设置根据这个数字设置年度季度月度对应日期
        let olddays3 = 0 // 设置根据这个数字设置年度季度月度对应日期
        let olddays4 = 0 // 设置根据这个数字设置年度季度月度对应日期
        for (let i = 0; i < 12; i++) {
          if (i === 0) {
            if (startMouth + i > 12) {
              days = days + this.isday(startMouth + i - 12, statYear + 1)
              olddays = this.isday(startMouth + i - 12, statYear + 1)
            } else {
              days = days + this.isday(startMouth + i, statYear)
              olddays = this.isday(startMouth + i, statYear)
            }
            this.tenlist.push({
              bytperiod: (i + 1),
              strstartdate: this.addForm.strstartdate,
              strenddate: statYear + '-' + this.iszeao(startMouth + i) + '-' + (this.blnnaturalmonth === false ? this.iszeao(this.addForm.straccountdate) : this.isday(startMouth + i, statYear)),
              blnisinactive: (this.dateType(this.addForm.stropendate, statYear + '-' + this.iszeao(startMouth + i) + '-' + (this.blnnaturalmonth === false ? this.iszeao(this.addForm.straccountdate) : this.isday(startMouth + i, statYear))).show) ? 1 : 0
            })
          } else if (i === 11) {
            if (startMouth + i > 12) {
              this.quarterList.push({
                strperiod: '第四季度',
                strstartdate: statYear + '-' + this.iszeao(startMouth + i - 3) + '-' + (this.blnnaturalmonth === false ? this.iszeao(this.addForm.straccountdate + 1) : this.isday(startMouth + i, statYear)),
                strenddate: this.addForm.strenddate,
                strstartperiod: '第10期间',
                strendperiod: '第12期间'
              })
              this.yearList.push({
                strperiod: '下半年',
                strstartdate: statYear + '-' + this.iszeao(startMouth + i - 6) + '-' + (this.blnnaturalmonth === false ? this.iszeao(this.addForm.straccountdate + 1) : this.isday(startMouth + i, statYear)),
                strenddate: this.addForm.strenddate,
                strstartperiod: '第7期间',
                strendperiod: '第12期间'
              })
            } else {
              this.quarterList.push({
                strperiod: '第四季度',
                strstartdate: this.getNewDate1(jzrdata, olddays4),
                strenddate: this.addForm.strenddate,
                strstartperiod: '第10期间',
                strendperiod: '第12期间'
              })
              this.yearList.push({
                strperiod: '下半年',
                strstartdate: this.getNewDate1(jzrdata, olddays3),
                strenddate: this.addForm.strenddate,
                strstartperiod: '第7期间',
                strendperiod: '第12期间'
              })
            }
            this.tenlist.push({
              bytperiod: (i + 1),
              strstartdate: this.getNewDate1(jzrdata, olddays),
              strenddate: this.addForm.strenddate,
              blnisinactive: (this.dateType(this.addForm.stropendate, this.addForm.strenddate).show) ? 1 : 0
            })
          } else {
            if (i === 1) {
              this.tenlist.push({
                bytperiod: (i + 1),
                strstartdate: this.getNewDate2(jzrdata, olddays),
                strenddate: this.getNewDate(jzrdata, olddays),
                blnisinactive: this.dateType(this.addForm.stropendate, this.getNewDate(jzrdata, olddays)).show ? 1 : 0
              })
            } else {
              this.tenlist.push({
                bytperiod: (i + 1),
                strstartdate: this.getNewDate1(jzrdata, olddays),
                strenddate: this.getNewDate(jzrdata, days),
                blnisinactive: this.dateType(this.addForm.stropendate, this.getNewDate(jzrdata, days)).show ? 1 : 0
              })
            }
            if (i === 2) {
              this.quarterList.push({
                strperiod: '第一季度',
                strstartdate: this.addForm.strstartdate,
                strenddate: this.getNewDate(jzrdata, days),
                strstartperiod: '第1期间',
                strendperiod: '第3期间'
              })
              olddays2 = days
            }
            if (i === 5) {
              this.quarterList.push({
                strperiod: '第二季度',
                strstartdate: this.getNewDate1(jzrdata, olddays2),
                strenddate: this.getNewDate(jzrdata, days),
                strstartperiod: '第4期间',
                strendperiod: '第6期间'
              })
              olddays3 = days
              this.yearList.push({
                strperiod: '上半年',
                strstartdate: this.addForm.strstartdate,
                strenddate: this.getNewDate(jzrdata, days),
                strstartperiod: '第1期间',
                strendperiod: '第6期间'
              })
            }
            if (i === 8) {
              olddays4 = days
              this.quarterList.push({
                strperiod: '第三季度',
                strstartdate: this.getNewDate1(jzrdata, olddays3),
                strenddate: this.getNewDate(jzrdata, days),
                strstartperiod: '第7期间',
                strendperiod: '第9期间'
              })
            }
            if (startMouth + i > 12) {
              days = days + this.isday(startMouth + i - 12, statYear + 1)
              olddays = days - this.isday(startMouth + i - 12, statYear + 1)
            } else {
              days = days + this.isday(startMouth + i, statYear)
              olddays = days - this.isday(startMouth + i, statYear)
            }
          }
        }
      } else {
        this.$message.error('开始日期结束日期选择错误,请重新选择')
        this.tenlist = [] // 清空月度列表
        this.quarterList = [] // 清空季度列表
        this.yearList = [] // 清空年度列表
      }
    },
    // 设置自然月的方法（因那为自然月生成方式与非自然月生成算法不同，自然月需要固定死的结束日期，而非自然月需动态计算）
    calculationDateZ() {
      let statYear = '' // 开始年份)
      let startMouth = 1 // 月份开始
      let stateDate = '' // 日期开始
      const endMouth = '' // 月份结束
      let endDate = '' // 日期结束
      let beginTime = 0 // 初始化日期
      let endtime = 0 // 最后的结束日期
      if (this.blnnaturalmonth === false && this.addForm.straccountdate === '') {
        this.$message.error('请选择结账日')
        return false
      }
      // 获取开始日期各种日子
      if (this.addForm.strstartdate !== '') {
        var stateOne = new Date(this.addForm.strstartdate)
        statYear = stateOne.getFullYear()
        startMouth = stateOne.getMonth() + 1
        stateDate = stateOne.getDate()
      }
      // 获取结束日期各种日子
      if (this.addForm.strenddate !== '') {
        var endOne = new Date(this.addForm.strenddate)
        endDate = endOne.getDate()
        console.log(endMouth, endDate, endtime)
      }
      // 账套日期日子
      if (stateDate > this.addForm.straccountdate) {
        beginTime = startMouth
        endtime = beginTime + 12
      } else {
        beginTime = startMouth
      }
      if (this.addForm.straccountdate > endDate && this.blnnaturalmonth === false) {
        this.$message.error('开始日期结束日期选择错误,请重新选择')
        return false
      }
      this.setOpenDate()
      this.tenlist = []
      this.quarterList = []
      this.yearList = []
      for (let i = 0; i < 12; i++) {
        if (i === 0) {
          this.tenlist.push({
            bytperiod: (i + 1),
            strstartdate: this.addForm.strstartdate,
            strenddate: statYear + '-' + this.iszeao(startMouth + i) + '-' + this.isday(startMouth + i, statYear),
            blnisinactive: (this.dateType(this.addForm.stropendate, statYear + '-' + this.iszeao(startMouth + i) + '-' + this.isday(startMouth + i, statYear)).show) ? 1 : 0
          })
        } else if (i === 11) {
          this.tenlist.push({
            bytperiod: (i + 1),
            strstartdate: statYear + '-' + this.iszeao(startMouth + i) + '-01',
            strenddate: this.addForm.strenddate,
            blnisinactive: (this.dateType(this.addForm.stropendate, statYear + '-' + this.iszeao(startMouth + i) + '-01').show) ? 1 : 0
          })
          this.quarterList.push({
            strperiod: '第四季度',
            strstartdate: statYear + '-' + this.iszeao(startMouth + i - 3) + '-01',
            strenddate: this.addForm.strenddate,
            strstartperiod: '第10期间',
            strendperiod: '第12期间'
          })
          this.yearList.push({
            strperiod: '下半年',
            strstartdate: statYear + '-' + this.iszeao(startMouth + i - 5) + '-01',
            strenddate: this.addForm.strenddate,
            strstartperiod: '第7期间',
            strendperiod: '第12期间'
          })
        } else {
          this.tenlist.push({
            bytperiod: (i + 1),
            strstartdate: statYear + '-' + this.iszeao(startMouth + i) + '-01',
            strenddate: statYear + '-' + this.iszeao(startMouth + i) + '-' + this.isday(startMouth + i, statYear),
            blnisinactive: (this.dateType(this.addForm.stropendate, statYear + '-' + this.iszeao(startMouth + i) + '-' + this.isday(startMouth + i, statYear)).show) ? 1 : 0
          })
          if (i === 2) {
            this.quarterList.push({
              strperiod: '第一季度',
              strstartdate: this.addForm.strstartdate,
              strenddate: statYear + '-' + this.iszeao(startMouth + i) + '-' + this.isday(startMouth + i, statYear),
              strstartperiod: '第1期间',
              strendperiod: '第3期间'
            })
          }
          if (i === 5) {
            this.quarterList.push({
              strperiod: '第二季度',
              strstartdate: statYear + '-' + this.iszeao(startMouth + i - 2) + '-01',
              strenddate: statYear + '-' + this.iszeao(startMouth + i) + '-' + this.isday(startMouth + i, statYear),
              strstartperiod: '第4期间',
              strendperiod: '第6期间'
            })
            this.yearList.push({
              strperiod: '上半年',
              strstartdate: this.addForm.strstartdate,
              strenddate: statYear + '-' + this.iszeao(startMouth + i) + '-' + this.isday(startMouth + i, statYear),
              strstartperiod: '第1期间',
              strendperiod: '第6期间'
            })
          }
          if (i === 8) {
            this.quarterList.push({
              strperiod: '第三季度',
              strstartdate: statYear + '-' + this.iszeao(startMouth + i - 2) + '-01',
              strenddate: statYear + '-' + this.iszeao(startMouth + i) + '-' + this.isday(startMouth + i, statYear),
              strstartperiod: '第7期间',
              strendperiod: '第9期间'
            })
          }
        }
      }
    },
    // 判断该年度是否是瑞年，2月份29日
    isyear(year) {
      if (year % 4 === 0 && year % 100 !== 0 || year % 400 === 0) {
        return true
      } else {
        return false
      }
    },
    // 格式化标准日期
    getNewDate(data, i) {
      var date1 = new Date(data).getTime()
      var date2 = date1 + (24 * 3600 * 1000 * i)
      var date3 = new Date(date2)
      return (date3.getFullYear() + '-' + this.iszeao(date3.getMonth() + 1) + '-' + this.iszeao(date3.getDate()))
    },
    // 格式化日期+1个月的日期
    getNewDate1(data, i) {
      var date1 = new Date(data).getTime()
      var date2 = date1 + (24 * 3600 * 1000 * (i + 1))
      var date3 = new Date(date2)
      return (date3.getFullYear() + '-' + this.iszeao(date3.getMonth() + 1) + '-' + this.iszeao(date3.getDate()))
    },
    // 格式化日期当前日期加1天的日期
    getNewDate2(data, i) {
      var date1 = new Date(data).getTime()
      var date2 = date1 + (24 * 3600 * 1000)
      var date3 = new Date(date2)
      return (date3.getFullYear() + '-' + this.iszeao(date3.getMonth() + 1) + '-' + this.iszeao(date3.getDate()))
    },
    // 开始日期改变
    changeYearStart(e) {
      this.calculationDate()
    },
    // 结束日期改变
    changeYearEnd(e) {
      this.calculationDate()
    },
    // 判断是否需要添加0来格式化日期
    iszeao(value) {
      if (value > 9) {
        return value
      } else {
        return '0' + value
      }
    },
    // 判断当前月有多天
    isday(value, yese) {
      const cas1 = [1, 3, 5, 7, 8, 10, 12]
      const cas2 = [4, 6, 9, 11]
      const cas3 = [2]
      if (cas1.includes(value)) {
        return 31
      }
      if (cas2.includes(value)) {
        return 30
      }
      if (cas3.includes(value) && this.isyear(yese)) {
        return 29
      }
      if (cas3.includes(value) && !this.isyear(yese)) {
        return 28
      }
    },
    // 判断是第一个日期是否大于第二个日期// 并返回状态以及相差天数
    dateType(date1, date2) {
      var oDate1 = new Date(date1)
      var oDate2 = new Date(date2)
      if (oDate1.getTime() >= oDate2.getTime()) {
        return {
          show: true,
          days: (oDate1 - oDate2) / (1000 * 60 * 60 * 24)
        }
      } else {
        return {
          show: false,
          days: (oDate2 - oDate1) / (1000 * 60 * 60 * 24)
        }
      }
    },
    // 新增
    saveCalendar(addForm, isMulti) {
      this.$refs.addForm.validate(valid => {
        if (valid) {
          const news = addForm
          news.accountperiodList = this.tenlist
          saveCalendar(news).then(res => {
            if (res.code === 20000) {
              this.$message.success(res.message)
              if (isMulti !== 'multi') {
                this.dialog.isShow = false
              } else {
                this.resetForm()
              }
              this.$emit('refresh')
            }
          })
        }
      })
    },
    // form表单清空
    resetForm() {
      this.addForm = Object.assign({}, this.initAddForm)
      this.$nextTick(() => {
        this.$refs['addForm'].clearValidate()
        this.tenlist = []
        this.quarterList = []
        this.yearList = []
        this.blnnaturalmonth = false
        this.opendateDisabled = false
      })
    },
    openDialog() {
      this.resetForm()
      this.getAddMessage()
    }
  }
}
</script>
<style>
.calendar-add  .el-table{
    margin-top: 20px
}
.calendar-add  .el-tabs--card>.el-tabs__header .el-tabs__item.is-active {
  background: #00A99D;
  color:#fff
}
.calendar-add .el-tabs__header {
  background: #F5F4F7;
  margin: 0px;
}
.calendar-add .el-tabs__content {
  background: #fff;
  margin-bottom: 0px;
}
.calendar-add .el-checkbox{
  margin-top: 33px;
}

</style>

